<?php
declare (strict_types=1);

namespace app\controller;

use app\helpers\MyCodeService;
use app\traits\JsonResponse;
use think\exception\ValidateException;
use think\Request;

class Code {
    use JsonResponse;
    public function send(Request $request){
        try {
            validate([
                'phone|手机号'  => 'require|regex:/^1[3589]\d{9}$/',
            ])->batch()->check($request->post());
            $code = app(MyCodeService::class)->send($request->post('phone'),'SMS_190793806');
            return $this->jsonSuccess(['code'=>$code],'短信发送成功~');
        } catch (ValidateException $e) {
            return $this->jsonFail($e->getError(),'输入信息不合法',422);
        }
    }

}
